<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="small"
          @click="handleSearch"
          >查询</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-circle-check"
          size="small"
          :disabled="single"
          @click="check_permit = true"
          v-hasPermi="['putStorage:proMatCheck:check']"
        >
          审核
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-circle-close"
          size="small"
          :disabled="single"
          @click="check_back = true"
          v-hasPermi="['putStorage:proMatCheck:back']"
        >
          撤回
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="small"
          :loading="exportLoading"
          @click="handleExport(1)"
          v-hasPermi="['putStorage:proMatStart:export']"
        >
          EXCEL
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="small"
          :loading="exportLoading"
          @click="handleExport(2)"
          v-hasPermi="['putStorage:proMatStart:export']"
        >
          子项EXCEL
        </el-button>
      </el-col>
      <Tag type="dot" size="default" color="#0fac09">提交</Tag>
      <Tag type="dot" size="default" color="#bb0331">通过</Tag>
      <Tag type="dot" size="default" color="#0323af">检验</Tag>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 头表Table -->
    <el-table
      @scrollBottom="load"
      highlight-current-row
      border
      stripe
      v-loading="loading"
      :height="tableH"
      :cell-style="iCellStyle2"
      @row-click="clickRtm"
      :data="proMatStartList"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" type="index" min-width="5" align="center" />
      <el-table-column
        label="退料单号"
        :show-overflow-tooltip="true"
        min-width="12"
        align="center"
        prop="rtmDjbh"
      />
      <el-table-column
        label="退料日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="rtmTlrq"
        min-width="12"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.rtmTlrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="退料部门"
        :show-overflow-tooltip="true"
        min-width="10"
        align="center"
        prop="rtmBmdm"
      />
      <el-table-column
        label="退料仓库"
        :show-overflow-tooltip="true"
        min-width="10"
        align="center"
        prop="rtmCkdm"
      >
        <template #default="scope">
          <dict-tag :options="allStorageOptions" :value="scope.row.rtmCkdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        :show-overflow-tooltip="true"
        min-width="12"
        align="center"
        prop="rtmDesc"
      />
      <el-table-column
        label="撤回原因"
        :show-overflow-tooltip="true"
        min-width="12"
        align="center"
        prop="rtmNgdesc"
      />
      <el-table-column
        label="开单人员"
        :show-overflow-tooltip="true"
        min-width="8"
        align="center"
        prop="rtmJlry"
      />
      <el-table-column
        label="开单日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="rtmJlrq"
        min-width="12"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.rtmJlrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="审核人员"
        :show-overflow-tooltip="true"
        min-width="8"
        align="center"
        prop="rtmShry"
      />
      <el-table-column
        label="审核日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="rtmShrq"
        min-width="12"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.rtmShrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="过账人员"
        :show-overflow-tooltip="true"
        min-width="8"
        align="center"
        prop="rtmGzry"
      />
      <el-table-column
        label="过账日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="rtmGzrq"
        min-width="12"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.rtmGzrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 子项Table   -->
    <el-table
      border
      stripe
      highlight-current-row
      :height="tableHChild"
      :data="rtdDetList"
      :header-cell-style="{ 'text-align': 'center', padding: '2px' }"
    >
      <el-table-column label="#" type="index" align="center" />
      <el-table-column label="物料代码" align="center" prop="rtdWldm" />
      <el-table-column label="规格描述" align="center" prop="itmPmgg" />
      <el-table-column
        label="单位"
        :show-overflow-tooltip="true"
        align="center"
        prop="itmDw"
      >
        <template #default="scope">
          <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
        </template>
      </el-table-column>
      <el-table-column
        label="储位信息"
        :show-overflow-tooltip="true"
        align="center"
        prop="rtdCwdm"
      >
        <template #default="scope">
          <dict-tag :options="cWOptions" :value="scope.row.rtdCwdm" />
        </template>
      </el-table-column>
      <el-table-column label="最小包装" align="center" prop="itmMinbz" />
      <el-table-column label="包装数量" align="center" prop="rtdBzsl" />
      <el-table-column label="申请数量" align="center" prop="rtdSqsl" />
      <el-table-column label="入库数量" align="center" prop="rtdRksl" />
      <el-table-column label="入库人员" align="center" prop="rtdRkry" />
      <el-table-column label="入库日期" align="center" prop="rtdRkrq">
        <template #default="scope">
          <span>{{ parseTime(scope.row.rtdRkrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="450px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="88px"
      >
        <el-col :span="24">
          <el-form-item label="建立日期">
            <el-date-picker
              v-model="daterangeRtmJlrq"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              :picker-options="pickerOptions"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="退料单号" prop="rtmDjbh">
            <el-input
              v-model="queryParams.rtmDjbh"
              placeholder="请输入退料单号"
              clearable
              style="width: 240px"
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="入库仓库" prop="rtmCkdm">
            <el-select
              v-model="queryParams.rtmCkdm"
              style="width: 240px"
              clearable
              filterable
              placeholder="请选择仓库"
            >
              <el-option
                v-for="item in storageOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item prop="rtmZtbz">
            <RadioGroup v-model="queryParams.rtmZtbz">
              <Radio style="margin-left: 100px" label="3">未审核</Radio>
              <Radio style="margin-left: 20px" label="2">已审核</Radio>
              <Radio style="margin-left: 20px" label="4">全部</Radio>
            </RadioGroup>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <!-- 撤回 -->
    <Modal v-model="check_back" draggable scrollable width="300">
      <p slot="header" style="color: #f60; text-align: center">
        <Icon type="ios-information-circle"></Icon>
        <span>确认撤回重写？</span>
      </p>
      <div style="text-align: center">
        <p>撤回理由</p>
        <Input v-model="backReason" style="width: 75%" />
      </div>
      <div slot="footer">
        <Button type="error" size="large" long @click="handleChange(1, 0)"
          >撤回</Button
        >
      </div>
    </Modal>
    <!-- 审核 -->
    <Modal v-model="check_permit" draggable scrollable width="300">
      <p slot="header" style="color: #f60; text-align: center">
        <Icon type="ios-information-circle"></Icon>
        <span>确认</span>
      </p>
      <div style="text-align: center">
        <p>确认将【入库单号：{{ rtmDjbhs }}】的材料进行审核作业吗？</p>
        <p>一旦操作无法更改，材料将进入仓库过账作业，请谨慎操作。</p>
      </div>
      <div slot="footer">
        <Button type="success" size="large" long @click="handleChange(2, 1)"
          >审核</Button
        >
      </div>
    </Modal>
  </div>
</template>

<script>
import {
  listProMat,
  exportProMat,
  listRtdDet,
  updateProMat,
} from "@/api/putStorage/proMatStart";
import { getAll, getstorageType } from "@/api/repertory/Storage";

export default {
  name: "ProMatCheck",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      rtmDjbhs: null,
      tableH: (document.body.clientHeight - 130) / 2,
      tableHChild: (document.body.clientHeight - 280) / 2,
      // 非单个禁用
      single: true,
      //审核、撤回
      check_back: false,
      check_permit: false,
      //撤回理由
      backReason: null,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      //所有存储仓库
      allStorageOptions: [],
      storageOptions: [],
      //单位字典
      unitFlagOptions: [],
      //默认储位字典
      cWOptions: [],
      // 总条数
      total: 0,
      // 生产退料入库表格数据
      proMatStartList: [],
      //生产退料入库详细表数据
      rtdDetList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 建立日期时间范围
      daterangeRtmJlrq: [
        new Date(new Date().getFullYear(), new Date().getMonth() - 1),
        this.$moment().endOf("month").toDate(),
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        rtmDjbh: null,
        rtmZtbz: "3",
        rtmJlrq: null,
      },
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    getAll().then((response) => {
      this.allStorageOptions = response.data;
      for (let item of this.allStorageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
    //单位
    this.getDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });
    //存放位置
    getAll().then((response) => {
      this.cWOptions = response.data;
      for (let item of this.cWOptions) {
        item.dictLabel = item.storageDefaultstorage;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
    getstorageType("0").then((response) => {
      this.storageOptions = response.data;
      for (let item of this.storageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
  },
  methods: {
    /** 查询生产退料入库列表 */
    getList(row) {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeRtmJlrq && "" != this.daterangeRtmJlrq) {
        this.queryParams.params["beginRtmJlrq"] = this.moment(
          this.daterangeRtmJlrq[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endRtmJlrq"] = this.moment(
          this.daterangeRtmJlrq[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      this.queryParams.pageNum = 1;
      listProMat(this.queryParams).then((response) => {
        this.proMatStartList = [];
        this.rtdDetList = [];
        this.proMatStartList = this.proMatStartList.concat(response.rows);
        this.total = response.total;
        this.loading = false;
        if (row != null) {
          this.clickRtm(row);
        } else if (
          this.proMatStartList != null &&
          this.proMatStartList.length > 0
        ) {
          this.clickRtm(this.proMatStartList[0]);
        }
      });
    },
    iCellStyle2: function ({ row, column, rowIndex, columnIndex }) {
      //提交
      if (row.rtmZtbz == "1" && row.rtmTjflag == "1") {
        return "color:#0fac09";
      } else if (row.rtmTjflag == "1" && row.rtmZtbz == "6") {
        return "color:#bb0331";
      } else if (row.rtmTjflag == "1" && row.rtmZtbz == "2") {
        return "color:#0323af";
      }
    },
    //load
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 20;
        this.queryParams.pageNum++;
        listProMat(this.queryParams).then((response) => {
          this.proMatStartList = this.proMatStartList.concat(response.rows);
          this.total = response.total;
          this.loading = false;
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.proMatStartList = [];
      this.getList();
      this.openSearch = false;
    },
    // 表单重置
    reset() {
      this.form = {
        rtmId: null,
        rtmDjbh: null,
        rtmTlrq: new Date(),
        rtmCkdm: null,
        rtmBmdm: null,
        rtmDesc: null,
        rtmTjflag: null,
        rtmNgdesc: null,
        rtmZtbz: null,
        rtmJlry: null,
        rtmJlrq: null,
        rtmShry: null,
        rtmShrq: null,
        rtmGzry: null,
        rtmGzrq: null,
      };
      this.resetForm("form");
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeRtmJlrq = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 单据单击事件*/
    clickRtm(row) {
      if (row != null) {
        this.ids = row.rtmId;
        this.rtmDjbhs = row.rtmDjbh;
        this.single = row == null || row.rtmZtbz != 1 || row.rtmTjflag != 1;
        this.getListRtdDet(row.rtmDjbh);
      }
    },
    getListRtdDet(rtmDjbh) {
      listRtdDet(rtmDjbh).then((res) => {
        this.rtdDetList = res.data;
      });
    },
    handleSearch() {
      this.reset();
      this.title = "查询生产退料入库";
      this.openSearch = true;
    },
    /** 审核、撤回操作按钮 */
    handleChange(ztbz, flag) {
      const data = {
        rtmId: this.ids,
        rtmZtbz: ztbz,
        rtmTjflag: flag,
        rtmNgdesc: this.backReason,
        rtmShry: this.$store.state.user.nickName,
        rtmShrq: new Date(),
      };
      updateProMat(data).then((res) => {
        if (ztbz == "1") {
          this.msgSuccess("撤回成功~");
          this.check_back = false;
        } else {
          this.msgSuccess("审核成功~");
          this.check_permit = false;
        }
        this.backReason = null;
        this.getList();
      });
    },
    /** 导出按钮操作 */
    handleExport(index) {
      if (index == 2 && this.rtmDjbhs == null) {
        this.msgError("导出子项前,请先选择单据");
        return;
      }
      let queryParams = this.queryParams;
      queryParams.index = index;
      queryParams.rtmDjbh = this.rtmDjbhs;
      exportProMat(queryParams).then((response) => {
        this.download(response.msg);
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
